package com.coollang.smartbra.activity;

import android.view.KeyEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.bleutil.BleUtil;
import com.coollang.smartbra.bleutil.callback.BleCallbackImpl;
import com.coollang.smartbra.presenter.SetHeartRateActivityPresenter;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;
import com.coollang.smartbra.utils.UIUtils;
import com.coollang.smartbra.views.CountView;
import com.github.ybq.android.spinkit.SpinKitView;
import com.kyleduo.switchbutton.SwitchButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jeesoft.widget.pickerview.LoopListener;
import cn.jeesoft.widget.pickerview.LoopView;

/**
 * 心率提醒页面
 */

public class SetHeartRateActivity extends MyAutoLayoutActivity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
    private static final int CURRENTHEARTRATE = 0;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.count_heart_rate)
    CountView heartRate;
    @BindView(R.id.sb_ios)
    SwitchButton sBtn;
    @BindView(R.id.iv_set_rate)
    ImageView ivSetRate;
    @BindView(R.id.tv_alarm_heart_rate)
    TextView tvAlarmRate;
    @BindView(R.id.rl_set_rate)
    RelativeLayout rlSetRate;
    @BindView(R.id.picklerview)
    LoopView lv;
    @BindView(R.id.ll_pick)
    RelativeLayout llPick;
    @BindView(R.id.tv_pick_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_pick_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_heart_rate_tips)
    TextView tvTips;
    @BindView(R.id.tv_heart_state)
    TextView mTvHeartState;
    @BindView(R.id.status_bar)
    View statusBar;
    @BindView(R.id.content)
    RelativeLayout content;
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.spin_kit)
    SpinKitView spinKit;
    private List<String> dataList = new ArrayList<>();
    private int firstIndex = -1;
    private String originalData;
    private String wheelData;
    private SetHeartRateActivityPresenter mActivityPresenter;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_set_heartrate);
        ButterKnife.bind(this);
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusBar, this, content);

        initViews();

        initListener();
        mActivityPresenter = new SetHeartRateActivityPresenter(this);

        String isalarm = MyApplication.mLogInDataBean.getIsalarm();
        if (Integer.parseInt(isalarm) == 1) {
            sBtn.setChecked(true);
        } else {
            sBtn.setChecked(false);
        }
        //进入页面默认启动心率检测
        setAlarmHeartRate(Integer.parseInt(isalarm));
    }

    private void initViews() {
        tvTitle.setText(R.string.heart_rate_alarm);
        tvTitle.setTextColor(UIUtils.getColor(R.color.black_80));
        tvTitle.setTextSize(16);
        tvAlarmRate.setText(MyApplication.mLogInDataBean.getHr_alarm() + " bmp");
        ivSetRate.setVisibility(View.INVISIBLE);
    }

    private void initListener() {
        sBtn.setOnCheckedChangeListener(this);
        rlSetRate.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);
        mIvBack.setOnClickListener(this);

        lv.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                wheelData = dataList.get(lv.getCurrentItem());
                setSpecialData(item);
            }
        });
        lv.setTextSize(24f);
    }


    @Override
    protected void onResume() {
        super.onResume();
        BleUtil.getInstance().registerNotifyListener(mBleCallback);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleUtil.getInstance().unRegisterNotifyListener();
    }

    //*****************************************请求蓝牙后回调*********************************************//

    private BleCallbackImpl mBleCallback = new BleCallbackImpl() {
        @Override
        public void onBleConnected() {
            MyApplication.getApplication().sendDate();// 校准时间戳
        }

        @Override
        protected void onHeartRateRecieved(int alarm, int alarmHeartRate, int currentHeartRate) {
            heartRate.setText(currentHeartRate + "");
            spinKit.setVisibility(View.GONE);
            mTvHeartState.setText("bpm");
            heartRate.setVisibility(View.VISIBLE);
        }
    };

    private void setSpecialData(int item) {
        wheelData = String.valueOf(item);
    }

    @Override
    protected void initData() {
        showPickView();
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

        if (b) {
            tvAlarmRate.setVisibility(View.VISIBLE);
            ivSetRate.setVisibility(View.INVISIBLE);
            MyApplication.mLogInDataBean.setIsalarm("1");
            mActivityPresenter.setRequestIsAlarm(1);
            setAlarmHeartRate(1);
        } else {

            tvAlarmRate.setVisibility(View.INVISIBLE);
            ivSetRate.setVisibility(View.VISIBLE);
            MyApplication.mLogInDataBean.setIsalarm("0");
            mActivityPresenter.setRequestIsAlarm(0);
            setAlarmHeartRate(0);
        }
    }

    private void setAlarmHeartRate(int alarmBoolean) {
        byte b;
        if (alarmBoolean == 1) {
            b = (byte) 0x01;
        } else {
            b = (byte) 0x00;
        }
        Integer iO = new Integer(Integer.parseInt((tvAlarmRate.getText().toString()).split(" bmp")[0]));
        byte bbs = iO.byteValue();
        BleUtil.getInstance().sendData((byte) 0x40, (byte) 0x00, b, bbs);//默认 不开启提醒 默认心率报警为 128
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_set_rate:
                rlSetRate.setVisibility(View.GONE);
                llPick.setVisibility(View.VISIBLE);
                tvTips.setVisibility(View.GONE);
                break;

            case R.id.tv_pick_cancel:
                rlSetRate.setVisibility(View.VISIBLE);
                llPick.setVisibility(View.GONE);
                tvTips.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_pick_confirm:
                rlSetRate.setVisibility(View.VISIBLE);
                llPick.setVisibility(View.GONE);
                tvTips.setVisibility(View.VISIBLE);

                String currentItem = dataList.get(lv.getCurrentItem());
                mActivityPresenter.setRequestHrAlarm(currentItem);
                //一切以本地为主
                MyApplication.mLogInDataBean.setHr_alarm(currentItem);
                tvAlarmRate.setText(currentItem + " bmp");
                setAlarmHeartRate(Integer.parseInt(MyApplication.mLogInDataBean.getIsalarm()));
                break;
            case R.id.iv_back:
                onBack();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBack();
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    private void onBack() {
        BleUtil.getInstance().sendData((byte) 0x40, (byte) 0x02);
        finish();
    }

    /**
     * 显示选项卡
     */
    private void showPickView() {
        for (int i = 60; i < 230; i++) {
            dataList.add(i + "");
            if (originalData != null && String.valueOf(i).equals(originalData)) {
                firstIndex = i - 100;
            }
        }
        lv.setHeartTextColor();
        lv.setNotLoop();
        lv.setMiddleLineVisible(false);
        lv.setArrayList(dataList);
        if (firstIndex != -1) {
            lv.setCurrentItem(firstIndex);
        }
    }
}
